package com.dc.config.handler;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.dc.common.core.LoginUser;
import com.dc.common.utils.ShiroUtil;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * @author zhuangcy
 * @date 2021/6/1
 * @description mybatisPlus 新增修改时自动插入字段数据
 */
@Component
public class MybatisMetaObjectHandler implements MetaObjectHandler {
    @Override
    public void insertFill(MetaObject metaObject) {
        LoginUser userInfo = ShiroUtil.getUserInfo();
        this.setFieldValByName("createdTime", new Date(), metaObject);
        this.setFieldValByName("createdBy", userInfo.getUserName(), metaObject);
        this.setFieldValByName("createdId", userInfo.getUserId(), metaObject);
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        LoginUser userInfo = ShiroUtil.getUserInfo();
        this.setFieldValByName("updatedTime", new Date(), metaObject);
        this.setFieldValByName("updatedBy", userInfo.getUserName(), metaObject);
        this.setFieldValByName("updatedId", userInfo.getUserId(), metaObject);
    }
}
